Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
lodash.debounce
Advanced tools
The lodash.debounce package provides a function that can delay the execution of a function until after wait milliseconds have elapsed since the last time it was invoked. It is useful for implementing behavior that should only happen after a delay, such as waiting for a pause in typing or reducing the frequency of API calls.
Debouncing function calls
This code sample shows how to debounce a function that saves input data. The saveInput function will only be called once every 250 milliseconds, no matter how many times the event is triggered.
const debounce = require('lodash.debounce');
const saveInput = debounce((value) => {
// Save the input value
console.log('Saving data:', value);
}, 250);
// Will only trigger the saveInput function once every 250ms
document.getElementById('input').addEventListener('keyup', (event) => {
saveInput(event.target.value);
});
Canceling a debounced call
This code sample demonstrates how to cancel a debounced function call. If the expensiveOperation is debounced and then canceled before the debounce time has elapsed, the function will not be executed.
const debounce = require('lodash.debounce');
const expensiveOperation = debounce(() => {
// Perform an expensive operation
console.log('Expensive operation executed');
}, 1000);
expensiveOperation();
expensiveOperation.cancel();
Immediate invocation
This code sample illustrates how to invoke the debounced function immediately on the leading edge of the timeout, without waiting for the trailing edge. The trackEvent function will be called immediately on the first click, but subsequent clicks within 200 milliseconds will not invoke the function again.
const debounce = require('lodash.debounce');
const trackEvent = debounce((event) => {
// Track the event
console.log('Event tracked:', event);
}, 200, {
'leading': true,
'trailing': false
});
// Will trigger on the leading edge instead of the trailing edge
document.getElementById('button').addEventListener('click', (event) => {
trackEvent('Button clicked');
});
The throttle-debounce package provides both throttle and debounce functions. It is similar to lodash.debounce but also includes a throttle function, which is useful for rate-limiting function calls.
debounce-promise is a package that debounces a function that returns a promise. It is similar to lodash.debounce but is specifically tailored for use with promises, aggregating multiple calls into a single promise.
debounce-fn is a package that offers debouncing functionality. It is similar to lodash.debounce but provides a simpler API and is built as an ESM-only module, which means it can only be imported using ES module syntax.
The modern build of lodash’s _.debounce
exported as a Node.js/io.js module.
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash.debounce
In Node.js/io.js:
var debounce = require('lodash.debounce');
See the documentation or package source for more details.
FAQs
The lodash method `_.debounce` exported as a module.
We found that lodash.debounce demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.